package com.kuqi.fragments;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.kuqi.R;
import com.kuqi.core.ApplicationManager;
import com.kuqi.core.ApplicationPersistent;
import com.kuqi.entitiy.UserInfo;
import com.kuqi.utils.ConvertTool;

import java.util.Base64;

/**
 * 用来模拟切换界面的小界面(后期会用其他事物替换)
 */
public class UserPageFragment extends Fragment {

    // 登录头像的背景图
    private ImageView Image_back;
    // 关键提示
    private TextView login_admin_msg;
    // 用户名
    private TextView user_name_tx;
    // 性别
    private TextView user_sex_tx;
    // 电话
    private TextView user_phone_tx;
    // 薪酬
    private TextView user_salary_tx;
    // 经验
    private TextView user_experience_tx;
    // 密匙
    private TextView user_VTK_tx;
    // 权限警告
    private TextView user_effective_tx;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        // 请注意attachToRoot参数,如果设置为true会造成循环调用,导致堆栈溢出
        View user_view = inflater.inflate(R.layout.fragment_user, container ,false);

        // 获得背景图图像控件
        Image_back = user_view.findViewById(R.id.Image_back);
        login_admin_msg = user_view.findViewById(R.id.login_admin_msg);
        user_name_tx = user_view.findViewById(R.id.user_name_tx);
        user_sex_tx = user_view.findViewById(R.id.user_sex_tx);
        user_phone_tx = user_view.findViewById(R.id.user_phone_tx);
        user_salary_tx = user_view.findViewById(R.id.user_salary_tx);
        user_experience_tx = user_view.findViewById(R.id.user_experience_tx);
        user_VTK_tx = user_view.findViewById(R.id.user_VTK_tx);
        user_effective_tx = user_view.findViewById(R.id.user_effective_tx);

        // 填写并展现当前用户的相关信息
        shownUserInfo();

        return user_view;
    }


    /**
     * 调整并展现用户信息
     */
    private void shownUserInfo()
    {
        UserInfo userInfo = ApplicationPersistent.getApplication().getUserInfo();
        Log.e("user",userInfo.toString());
        user_name_tx.setText(userInfo.getUserName());
        user_sex_tx.setText(userInfo.getUserSex());
        user_phone_tx.setText(userInfo.getPhone());
        user_salary_tx.setText(userInfo.getSalary() + "RMB");
        user_experience_tx.setText(userInfo.getExperience() + "年");
        user_VTK_tx.setText(userInfo.getSecretKey());
        user_effective_tx.setText(String.format("当前用户权限还有%s天过期。", userInfo.getEffective()));

        if(userInfo.getUserAgeLimit() == 1){
            login_admin_msg.setText("【访客模式】");
            // 访客模式就先正经点，不搞自定义头像了
            // Bitmap user_img = ConvertTool.base64ToBitmap(userInfo.getFileBase64());
            // Image_back.setImageBitmap(user_img);
        }else{
            login_admin_msg.setText("【老色批模式】");
            Image_back.setImageDrawable(getResources().getDrawable(R.drawable.child_false,null));
        }
    }

}
